package xyz.thoughtset.viewer.modules.step.service;

import cn.zhxu.bs.MapSearcher;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.ObjectUtils;
import xyz.thoughtset.viewer.common.crud.core.service.BaseServiceImpl;
import xyz.thoughtset.viewer.modules.step.dao.QueryBlockDao;
import xyz.thoughtset.viewer.modules.step.dao.BlockParamDao;
import xyz.thoughtset.viewer.modules.step.entity.BlockParam;
import xyz.thoughtset.viewer.modules.step.entity.QueryBlock;
import xyz.thoughtset.viewer.modules.step.entity.vo.QueryBlockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Service()
@Transactional
public class QueryBlockServiceImpl extends BaseServiceImpl<QueryBlockDao, QueryBlock> implements QueryBlockService {
    @Autowired
    private BlockParamDao blockParamDao;
    @Autowired
    private MapSearcher mapSearcher;

    @Override
    public Object saveData(LinkedHashMap<String, Object> baseMap) {
        QueryBlock entity = (QueryBlock) super.saveData(baseMap);
        List<BlockParam> oldParams = blockParams(entity.getId());
        HashSet params = new HashSet<>();
        for (BlockParam param : entity.getParamMappings()){
            param.setPid(entity.getId());
            blockParamDao.insertOrUpdate(param);
            params.add(param.getId());
        }
        if (!ObjectUtils.isEmpty(oldParams)){
            List<String> delIds = oldParams.stream()
                    .filter(e->!params.contains(e.getId()))
                    .map(BlockParam::getId)  // 提取name属性
                    .collect(Collectors.toList());
            blockParamDao.deleteByIds(delIds);
        }
        return entity;
    }

    @Override
    public boolean deleteById(String pkey) {
        blockParamDao.delete(this.pidQueryWrapper(pkey));
        return super.deleteById(pkey);
    }

    @Override
    public QueryBlock getById(Serializable id) {
        QueryBlock result = super.getById(id);
        result.setParamMappings(blockParams((String) id));
        return result;
    }

    @Override
    public Object saveBlockOrder(List<String> items) {
        Integer sortNum = 0;
        List<QueryBlock> blocks = new ArrayList<>(items.size());
        for (String item : items){
            blocks.add(QueryBlock.buildByOrder(item,++sortNum,QueryBlock.class));
        }
        return this.updateBatchById(blocks,5);
    }

    @Override
    public List blockParams(String blockId) {
        QueryWrapper<BlockParam> query = new QueryWrapper();
        query.lambda().eq(BlockParam::getPid,blockId).orderByDesc(BlockParam::getUpdatedAt);
        return blockParamDao.selectList(query);
    }

    @Override
    public List selectList(LinkedHashMap<String, Object> baseMap) {
        Map paramMap = cn.zhxu.bs.util.MapUtils.builder(baseMap)
                .orderBy(QueryBlockVo::getOrderNum).asc()
                .orderBy(QueryBlockVo::getUpdatedAt).desc().build();
        return mapSearcher.searchList(QueryBlockVo.class,paramMap);
    }

    @Override
    public List listByPId(String pid) {
        QueryWrapper<QueryBlock> query = new QueryWrapper();
        query.lambda().eq(QueryBlock::getPid,pid)
                .orderByAsc(QueryBlock::getOrderNum)
                .orderByDesc(QueryBlock::getUpdatedAt);
        return list(query);
    }
}
